package com.hujun.common.time.common;

import java.util.Date;

import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class DateTimeUtils {
	/**
	 * 日期格式化，默认格式为“yyyy-MM-dd”
	 * @param date
	 * @return
	 */
	public static String formateDateToStr(Date date) {
		if(null == date){
			return null;
		}
		DateTime dateTime  = new DateTime(date);
		return dateTime.toString("yyyy-MM-dd");
	}
	/**
	 * 按指定的format格式化日期
	 * @param date
	 * @param formatStr
	 * @return
	 */
	public static String formateDateToStr(Date date, String formatStr) {
		if(null == date || formatStr == null){
			return null;
		}
		DateTime dateTime = new DateTime(date);
		return dateTime.toString(formatStr);
	}
	
	/**
	 * 将字符串转换成Date，默认格式为"yyyy-MM-dd"
	 * @param date
	 * @return
	 */
	public static Date parseToDate(String date){
		return parseToDate(date, null);
	}
	/**
	 * 按指定的format转换日期
	 * @param date
	 * @param format
	 * @return
	 */
	public static Date parseToDate(String date,String format){
		if(date == null){
			return null;
		}
		if(format == null){
			format = "yyyy-MM-dd";
		}
		DateTimeFormatter dateTimeFormat = DateTimeFormat.forPattern(format);
		return DateTime.parse(date,dateTimeFormat).toDate();
	}
	
	/**
	 * 加年，负数为减
	 * @param date
	 * @param years
	 * @return
	 */
	public static Date plusYear(Date date,int years){
		if(null == date){
			return null;
		}
		DateTime dateTime = new DateTime(date);
		if(years >= 0){
			return dateTime.yearOfCentury().addToCopy(years).toDate();
		}else{
			return dateTime.minusYears(Math.abs(years)).toDate();
		}
		
	}
	
	public static Date plusMonth(Date date,int months){
		if(null == date){
			return null;
		}
		DateTime dateTime = new DateTime(date);
		if(months >= 0){
			return dateTime.plusMonths(months).toDate();
		}else{
			return dateTime.minusMonths(Math.abs(months)).toDate();
		}
	}
	
	public static Date plusDayOfMonth(Date date,int days){
		if(null == date){
			return null;
		}
		DateTime dateTime = new DateTime(date);
		if(days >= 0){
			return dateTime.plusDays(days).toDate();
		}else{
			return dateTime.minusDays(Math.abs(days)).toDate();
		}
	}
	
	public static Date plusHours(Date date,int hours){
		if(null == date){
			return null;
		}
		DateTime dateTime = new DateTime(date);
		if(hours >= 0){
			return dateTime.plusHours(hours).toDate();
		}else{
			return dateTime.minusHours(Math.abs(hours)).toDate();
		}
	}
	
	public static Date plusMinutes(Date date,int minutes){
		if(null == date){
			return null;
		}
		DateTime dateTime = new DateTime(date);
		if(minutes >= 0){
			return dateTime.plusMinutes(minutes).toDate();
		}else{
			return dateTime.minusMinutes(Math.abs(minutes)).toDate();
		}
	}
	
	public static Date plusSeconds(Date date,int seconds){
		if(null == date){
			return null;
		}
		DateTime dateTime = new DateTime(date);
		if(seconds >= 0){
			return dateTime.plusSeconds(seconds).toDate();
		}else{
			return dateTime.minusSeconds(Math.abs(seconds)).toDate();
		}
	}
	/**
	 * 计算两个日期之间相差的天数，用date2-date1,如果date2<date1,则为负数，日期差，只算yyyy-MM-dd 部分，不算时间
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static Long getDaysOfTwoDate(Date date1,Date date2){
		if(null == date1 || null == date2){
			return null;
		}
		Duration duration = new Duration(date1.getTime(),date2.getTime());
		return duration.getStandardDays();
	}

	
	public static void main(String[] args) {

		System.out.println(plusSeconds(new Date(), -120));
		System.out.println(getDaysOfTwoDate(new Date(), parseToDate("2015-1-12")));
	}
	
}
